<template>
  <div class="userLog">
    <el-dialog :title="title" :visible.sync="showRole" width="520px" :before-close="handleClose" custom-class="dialog">
      <div v-loading="loading" class="marginTop">
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" style="width: 380px;" />
          </el-form-item>
          <el-form-item label="角色级别" prop="level">
            <el-input-number v-model.number="form.level" :min="1" controls-position="right" style="width: 145px;" />
          </el-form-item>
          <!-- <el-form-item label="数据范围" prop="dataScope">
            <el-select v-model="form.dataScope" style="width: 140px" placeholder="请选择数据范围" @change="changeScope">
              <el-option
                v-for="item in dateScopes"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item v-if="form.dataScope === '自定义'" label="数据权限" prop="depts">
            <treeselect
              v-model="form.depts"
              :load-options="loadDepts"
              :options="depts"
              multiple
              style="width: 380px"
              placeholder="请选择"
            />
          </el-form-item> -->
          <el-form-item label="描述信息" prop="description">
            <el-input v-model="form.description" style="width: 380px;" rows="5" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button :loading="btnLoading" type="primary" @click="submitRoleForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

// import Treeselect from '@riophae/vue-treeselect'
// import '@riophae/vue-treeselect/dist/vue-treeselect.css'
// import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import { getDepts, getDeptSuperior } from '@/api/system/dept'
import { add, edit } from '@/api/system/role'
export default {
  // components: { Treeselect },
  props: {
    showRole: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    editData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      depts: [],
      dateScopes: ['全部', '本级', '自定义'],
      loading: false,
      btnLoading: false,
      form: {
        id: null,
        name: null,
        level: 3,
        dataScope: '全部',
        depts: [],
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    showRole() {
      if (this.showRole) this.initForm()
    }
  },
  methods: {
    initForm() {
      let pamrms = {
        id: null,
        name: null,
        level: 3,
        dataScope: '全部',
        depts: [],
        description: ''
      }
      if (this.title === '编辑角色') {
        pamrms = Object.assign(this.form, this.editData)
        console.log('pamrms', pamrms)
        if (this.editData.dataScope === '自定义') {
          if (this.editData.id == null) {
            this.getDeptsList()
          } else {
            this.getSupDepts(this.editData.depts)
          }
        }
        const depts = []
        this.editData.depts.forEach(function(dept, index) {
          depts.push(dept.id)
        })
        this.editData.depts = depts
      }
      this.form = pamrms
    },
    // 如果数据权限为自定义则获取部门数据
    changeScope() {
      if (this.form.dataScope === '自定义') {
        this.getDeptsList()
      }
    },
    getSupDepts(depts) {
      const ids = []
      depts.forEach(dept => {
        ids.push(dept.id)
      })
      getDeptSuperior(ids).then(res => {
        this.depts = res.list.map(function(obj) {
          if (obj.hasChildren && !obj.children) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 获取部门数据
    getDeptsList() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.list.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 获取弹窗内部门数据
    // loadDepts({ action, parentNode, callback }) {
    //   if (action === LOAD_CHILDREN_OPTIONS) {
    //     getDepts({ enabled: true, pid: parentNode.id }).then(res => {
    //       parentNode.children = res.list.map(function(obj) {
    //         if (obj.hasChildren) {
    //           obj.children = null
    //         }
    //         return obj
    //       })
    //       setTimeout(() => {
    //         callback()
    //       }, 200)
    //     })
    //   }
    // },
    // 关闭前
    handleClose() {
      this.$refs['form'].resetFields()
      this.$emit('update:showRole', false)
    },
    // 提交
    submitRoleForm(formName) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.title === '新增角色') {
            this.addRole()
          } else this.editRole()
          console.log(this.form)
        } else {
          return false
        }
      })
    },
    // 编辑
    editRole() {
      edit({ ...this.form }).then(res => {
        this.btnLoading = false
        this.handleClose()
        this.$refs['form'].resetFields()
        this.$emit('search')
        this.$message.success('操作成功~')
      })
    },
    // 新增
    addRole() {
      const depts = []
      if (this.form.dataScope === '自定义') {
        this.form.depts.forEach(function(data, index) {
          const dept = { id: data }
          depts.push(dept)
        })
        this.form.depts = depts
      }
      add({ ...this.form }).then(res => {
        this.btnLoading = false
        this.handleClose()
        this.$refs['form'].resetFields()
        this.$emit('search')
        this.$message.success('操作成功~')
      }).catch(() => {
        this.btnLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.el-form-item--small.el-form-item{
  margin-bottom: 25px;
}
</style>
